package com.zjxmyq.microserviceyida.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zjxmyq.domain.result.RestResponse;
import com.zjxmyq.microserviceyida.service.YiDaService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class YiDaController {
	@Resource
	YiDaService yiDaService;

	/**
	 * 测试代码
	 */
	@GetMapping("testyida")
	public RestResponse test() {
		return RestResponse.success(
				yiDaService.test()
		);
	}

	/**
	 * 获取实例Id列表分页
	 *
	 * @return
	 */
	@GetMapping("/getIdList")
	public RestResponse getIdList(Integer pageSize, Integer pageNumber) {
		return RestResponse.success(
				yiDaService.getIdList(pageSize, pageNumber)
		);
	}

	/**
	 * 获取总工对象
	 *
	 * @return
	 */
	@PostMapping("/getEngineerList")
	public RestResponse getEngineerList(String formInstId) {
		JSONObject engineerList = yiDaService.getEngineerList(formInstId);
		JSONObject fd = engineerList.getJSONObject("formData");
		//把formData中的数据转换为字符串
		String jsonString = fd.toString();
		//根据key获得固定的值
		JSONObject jsonObject = JSONObject.parseObject(jsonString);
		//对二级总进行遍历
		JSONArray arrName = jsonObject.getJSONArray("employeeField_lq4vxfji");
		JSONArray jsonArrayId = jsonObject.getJSONArray("employeeField_lq4vxfji_id");
		JSONArray secondEngineer = new JSONArray();
		for (int i = 0; i < arrName.size(); i++) {
			JSONObject sm = new JSONObject();
			String names = arrName.getString(i);
			String id = jsonArrayId.getString(i);
			sm.put("name", names);
			sm.put("id",id);
			secondEngineer.add(sm);
		}
		JSONArray arrName2 = jsonObject.getJSONArray("employeeField_lq4t9qtu");
		JSONArray jsonArrayId2 = jsonObject.getJSONArray("employeeField_lq4t9qtu_id");
		JSONArray thirdEngineer = new JSONArray();
		for (int i = 0; i < arrName2.size(); i++) {
			JSONObject sm = new JSONObject();
			String names = arrName2.getString(i);
			String id = jsonArrayId2.getString(i);
			sm.put("name", names);
			sm.put("id",id);
			thirdEngineer.add(sm);
		}
		JSONObject ob = new JSONObject();
		ob.put("secondEngineer", secondEngineer);
		ob.put( "thirdEngineer",thirdEngineer);
		return RestResponse.success(ob);
	}
}
